package com.th.supcom.pds.impl.service.internal.phamapply;

import java.util.List;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.vo.InpPhamApplyDetailSummaryVO;
import com.th.supcom.pds.impl.common.enums.PhamExtensionTypeIdEnum;
import com.th.supcom.pds.impl.common.utils.ExceptionThrowHelper;
import com.th.supcom.pds.impl.manager.InpApplyPhamInfoQueryManager;
import com.th.supcom.pds.internal.api.phamapply.InpPhamApplyInfoQueryService;

public class InpPhamApplyInfoQueryServiceImpl implements InpPhamApplyInfoQueryService
{
    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (InpPhamApplyInfoQueryServiceImpl.class);
    private InpApplyPhamInfoQueryManager m_inpApplyPhamInfoQueryManager;
    private TranslatorManager m_translatorManager;

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    public void setInpApplyPhamInfoQueryManager (InpApplyPhamInfoQueryManager inpApplyPhamInfoQueryManager)
    {
        m_inpApplyPhamInfoQueryManager = inpApplyPhamInfoQueryManager;
    }

    @Override
    public List <InpPhamApplyDetailSummary> findInpApplyBasicPhamInfo (InpPhamApplyDetailSummaryVO inpPhamApplyDetailSummaryVO)
    {
        s_logger.info ("调用findInpApplyBasicPhamInfo参数 ：inpPhamApplyDetailSummaryVO:{}", new Object[]
        { inpPhamApplyDetailSummaryVO });
        if (inpPhamApplyDetailSummaryVO == null)
        {
            throw new BusinessException ("通过药品标准码查询药品基本属性时，查询条件不能为空");
        }
        inpPhamApplyDetailSummaryVO.setLargeLiquidContTypeId (PhamExtensionTypeIdEnum.LargeLiquid.getEnumCode ());
        inpPhamApplyDetailSummaryVO.setOralPillsContTypeId (PhamExtensionTypeIdEnum.OralPills.getEnumCode ());
        inpPhamApplyDetailSummaryVO.setSplitTypeContTypeId (PhamExtensionTypeIdEnum.SplitType.getEnumCode ());
        inpPhamApplyDetailSummaryVO.setLargePackageSmallDosageContTypeId (PhamExtensionTypeIdEnum.LargePackageSmallDosage.getEnumCode ());
        try
        {
            return m_inpApplyPhamInfoQueryManager.findInpApplyBasicPhamInfo (inpPhamApplyDetailSummaryVO);
        }
        catch (Exception e)
        {
            s_logger.error ("通过药品标准码查询药品基本属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过药品标准码查询药品基本属性异常.", e);
        }
    }

    @Override
    public List <InpPhamApplyDetailSummary> findInpApplyPhamStockInfo (List <InpPhamApplyDetailSummary> list)
    {
        s_logger.info ("调用findInpApplyPhamStockInfo参数 ：list:{}", new Object[]
        { list });
        if (list.isEmpty ())
        {
            throw new BusinessException ("通过药品标准码查询药品库存和可供属性时，查询条件不能为空");
        }
        try
        {
            return m_inpApplyPhamInfoQueryManager.findInpApplyPhamStockInfo (list);
        }
        catch (Exception e)
        {
            s_logger.error ("通过药品标准码查询药品库存和可供属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过药品标准码查询药品库存和可供属性异常.", e);
        }
    }

    @Override
    public List <InpPhamApplyDetailSummary> findInpDispensePhamInfo (InpPhamApplyDetailSummaryVO inpPhamApplyDetailSummaryVO)
    {
        s_logger.info ("调用findInpDispensePhamInfo参数 ：inpPhamApplyDetailSummaryVO:{}", new Object[]
        { inpPhamApplyDetailSummaryVO });
        if (inpPhamApplyDetailSummaryVO == null)
        {
            throw new BusinessException ("药房摆药时，通过药品标准码List和摆药药房查询药品属性，查询条件不能为空");
        }
        try
        {
            return m_inpApplyPhamInfoQueryManager.findInpDispensePhamInfo (inpPhamApplyDetailSummaryVO);
        }
        catch (Exception e)
        {
            s_logger.error ("通过药品标准码List和摆药药房查询药品属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过药品标准码List和摆药药房查询药品属性异常.", e);
        }
    }

    @Override
    public List <InpPhamApplyDetailSummary> findAvailableDispensary (List <InpPhamApplyDetailSummary> list)
    {
        ExceptionThrowHelper.throwIllegalArgumentException (CollectionUtils.isEmpty (list), "查询药品可供并存在库存的药房时，参数对象不能为空");
        for (InpPhamApplyDetailSummary inpPhamApplyDetailSummary : list)
        {
            ExceptionThrowHelper.throwIllegalArgumentException (StringUtils.isBlank (inpPhamApplyDetailSummary.getPhamStdCode ()), "查询药品可供并存在库存的药房时,药品标准码不能为空");
            //ExceptionThrowHelper.throwIllegalArgumentException (StringUtils.isBlank (inpPhamApplyDetailSummary.getNurseUnit ()), "查询药品可供并存在库存的药房时,护理单元代码不能为空");
            //ExceptionThrowHelper.throwIllegalArgumentException (StringUtils.isBlank (inpPhamApplyDetailSummary.getApplySupplyFlag ()), "查询药品可供并存在库存的药房时,药品可供标志不能为空");
        }
        try
        {
            return m_translatorManager.translate (m_inpApplyPhamInfoQueryManager.findAvailableDispensary (list));
        }
        catch (Exception e)
        {
            s_logger.error ("查询药品可供并存在库存的药房异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药品可供并存在库存的药房异常.", e);
        }
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
